const pkg = require('ssh2')
const {
	Client
} = pkg;
module.exports = function({
	ip: serverIP,
	root: serverUsername,
	pwd: serverPassword,
}) {
	return new Promise((resolve, reject) => {
		if (!serverIP) return reject('serverIP not empty!!')
		if (!serverUsername) return reject('serverUsername not empty!!')
		if (!serverPassword) return reject('serverPassword not empty!!')

		const conn = new Client();
		conn.on('ready', () => {
			conn.sftp(async (error, sftp) => {
				if (error) {
					conn.end();
					return reject(`Error establishing SFTP connection: ${error}`)
				}
				resolve()
			});
		});

		conn.on('error', (error) => {
			console.log(error)
			reject(`服务器连接失败,ip:${serverIP},root:${serverUsername},pwd:${serverPassword}`)
		});

		conn.connect({
			host: serverIP,
			port: 22,
			username: serverUsername,
			password: serverPassword,
			readyTimeout: 60 * 1000
		});
	})

}